package com.example.module_hp_zao_yin.utils;

import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;

import com.example.module_hp_zao_yin.HpZaoYinMainFragment;

import java.lang.ref.WeakReference;

public class AudioRecordDemo {
    private static final String TAG = "AudioRecord";
    static final int SAMPLE_RATE_IN_HZ = 8000;
    static final int BUFFER_SIZE = AudioRecord.getMinBufferSize(SAMPLE_RATE_IN_HZ,
            AudioFormat.CHANNEL_IN_DEFAULT, AudioFormat.ENCODING_PCM_16BIT);
    AudioRecord mAudioRecord;
    boolean isGetVoiceRun;
    Object mLock;
    private WeakReference<HpZaoYinMainFragment> mActivity;

    public AudioRecordDemo(HpZaoYinMainFragment activity) {
        mLock = new Object();
        mActivity = new WeakReference<>(activity);
    }

    public void sotp() {
        this.isGetVoiceRun = false;
    }
    //对外暴露接口
    public interface MonitorInterface{
        void monitorListener(Double res);
    }
    public void getNoiseLevel(final MonitorInterface monitorInterface) {
        if (isGetVoiceRun) {
            return;
        }
        mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
                SAMPLE_RATE_IN_HZ, AudioFormat.CHANNEL_IN_DEFAULT,
                AudioFormat.ENCODING_PCM_16BIT, BUFFER_SIZE);
        if (mAudioRecord == null) {
        }
        isGetVoiceRun = true;
        new Thread(new Runnable() {
            @Override
            public void run() {
                mAudioRecord.startRecording();
                short[] buffer = new short[BUFFER_SIZE];
                final HpZaoYinMainFragment activity = mActivity.get();
                while (isGetVoiceRun) {
                    int r = mAudioRecord.read(buffer, 0, BUFFER_SIZE);
                    long v = 0;
                    for (int i = 0; i < buffer.length; i++) {
                        v += buffer[i] * buffer[i];
                    }
                    double mean = v / (double) r;
                    final double volume = 10 * Math.log10(mean);
                    Double d = new Double(volume);
                    Double res = Math.round (d * 10) / 10.0;
                    //Log.d(TAG, "录音db value:" + result);
                    //int res = d.intValue();
                    //Log.d(TAG, "db value:" + volume);
                    if (null != activity) {
                        activity.getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                monitorInterface.monitorListener(res<0?0:res);
                            }
                        });
                    }
                    // 大概一秒十次
                    synchronized (mLock) {
                        try {
                            mLock.wait(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
                mAudioRecord.stop();
                mAudioRecord.release();
                mAudioRecord = null;
                if (null != activity) {
                    activity.getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            monitorInterface.monitorListener(0.0);
                        }
                    });
                }
            }
        }).start();
    }
}
